
local Meta = FindMetaTable( "Entity" )
 
if not Meta then
    return
end
 
if not Meta.FireBulletsOld then
    Meta.FireBulletsOld = Meta.FireBullets
end
 
Meta.FireBullets = function( self, b )

	self:AddGuilt(math.random(5,10))
	
    self:FireBulletsOld( b )
 
end

function GuiltOnSpawn(ply)
	local GuiltNumber = 0
	
	if ply:GetPData("Guilt") then
		GuiltNumber = ply:GetPData("Guilt")
	end	
	
	ply.GuiltNumber = tonumber(GuiltNumber)
	ply:SendGuilt()
	
end

function GuiltOnDisconnect(ply)
	ply:SetPData("Guilt", ply.GuiltNumber)
end

function _R.Player:IsGuilty()
	return (self.GuiltNumber > 0) or false
end 

function _R.Player:AddGuilt(Amount)
	self.GuiltLowerTime = CurTime() + 120
	
	if Amount < 1 then return end
	
	local Amount = tonumber(Amount) + self.GuiltNumber
	
	self.GuiltNumber = tonumber(math.Clamp(self.GuiltNumber + Amount, 0, 100))
	self:SendGuilt()
	self:GuiltTimer()
	
end
	
function _R.Player:RemoveGuilt(Amount)
	self.GuiltNumber = math.Clamp(self.GuiltNumber - Amount, 0, 100)
	self:SendGuilt()
end

function _R.Player:SetGuilt(Amount)
	self.GuiltNumber = math.Clamp(Amount, 0, 100)
	self:SendGuilt()
end

function _R.Player:SendGuilt()
	umsg.Start( "RecieveGuilt", self )
		umsg.Char( self.GuiltNumber )
	umsg.End()
end

function _R.Player:GuiltTimer()
	timer.Create(self:UniqueID().."_GuiltTimer", 3, 0, function()
		if self.GuiltNumber < 1 then timer.Destroy(self:UniqueID().."_GuiltTimer") end
		self:RemoveGuilt(1)
	end)
end

function GuiltOnDeath(victim, weapon, killer)
	if killer and killer != victim then killer:AddGuilt(50) end
	victim:SetGuilt(0)
end

hook.Add("PlayerInitialSpawn", "GuiltSpawn", GuiltOnSpawn)
hook.Add("PlayerDisconnected", "GuiltDisconnect", GuiltOnDisconnect)
hook.Add("PlayerDeath", "GuiltDeath", GuiltOnDeath)